package com.ewili.x.special.task.thread.impl;

import com.ewili.framework.utils.DateUtils;
import com.ewili.framework.utils.ExceptionUtils;
import com.ewili.framework.utils.ObjectHttpClientUtils;
import com.ewili.x.special.dao.inter.AmountStockDao;
import com.ewili.x.special.entity.AmountStock;
import com.ewili.x.special.task.thread.inter.AmountStockThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

/**
 * @author wonder
 */
@Slf4j
@Component
public class AmountStockThreadTaskImpl implements AmountStockThreadTask {

    @Autowired
    private AmountStockDao amountStockDao;

    @Async("myTaskAsyncPool")
    @Override
    public void doSpide(String code) {
        String fullCode = code;
        if (fullCode.startsWith("00")) {
            fullCode += "2";
        } else if (code.startsWith("60")) {
            fullCode += "1";
        } else {
            fullCode += "2";
        }
        String url = "http://ff.eastmoney.com//EM_CapitalFlowInterface/api/js?type=hff&rtntype=2&js=({data:[(x)]})&cb=var%20aff_data=&check=TMLBMSPROCR&acces_token=1942f5da9b46b069953c873404aad4b5&id=" + fullCode + "&_=1565683238506";
        try {
            this.clearContent(code, url);
        } catch (Exception e) {
            log.error("url:{}", url, e);
        }

    }

    private void clearContent(String code, String url) {
        Connection connection = Jsoup.connect(url).timeout(10000).ignoreContentType(true);
        connection.maxBodySize(0);

        String content = null;
        try {
            content = connection.get().body().text();
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        }

        String jsonContent = null;
        try {
            jsonContent = ObjectHttpClientUtils.formatEastMoneyJson(content, "var aff_data=(", ")").getJSONArray("data").get(0).toString().replace("[", "").replace("]", "");
        } catch (IndexOutOfBoundsException e) {
            return;
        }

        String[][] contentArr = ObjectHttpClientUtils.formatEastMoneyJsonData(jsonContent);
        for (int i = 0; i < contentArr.length; i++) {
            String[] subContentArr = contentArr[i];
            Date date = null;
            try {
                date = DateUtils.parseDate(subContentArr[0].replace("\"", ""), DateUtils.yyyy_MM_dd);
            } catch (ParseException e) {
                ExceptionUtils.propagate(e);
            }
            Double mainNetInflow = Double.valueOf(subContentArr[1]);
            Double mainNetInflowRate = subContentArr[2].equalsIgnoreCase("-") ? 0.0d : Double.valueOf(subContentArr[2].replace("%", ""));


            Double largeNetInflow = Double.valueOf(subContentArr[3]);
            Double largeNetInflowRate = subContentArr[4].equalsIgnoreCase("-") ? 0.0d : Double.valueOf(subContentArr[4].replace("%", ""));

            Double bigNetInflow = Double.valueOf(subContentArr[5]);
            Double bigNetInflowRate = subContentArr[6].equalsIgnoreCase("-") ? 0.0d : Double.valueOf(subContentArr[6].replace("%", ""));


            Double midNetInflow = Double.valueOf(subContentArr[7]);
            Double midNetInflowRate = subContentArr[8].equalsIgnoreCase("-") ? 0.0d : Double.valueOf(subContentArr[8].replace("%", ""));


            Double smallNetInflow = Double.valueOf(subContentArr[9]);
            Double smallNetInflowRate = subContentArr[10].equalsIgnoreCase("-") ? 0.0d : Double.valueOf(subContentArr[10].replace("%", ""));


            Double chg = Double.valueOf(subContentArr[11]);
            Double close = Double.valueOf(subContentArr[12].replace("%", "").replace("\"", ""));

            AmountStock amountStock = amountStockDao.findByCodeAndDate(code, date);

            if (amountStock == null) {
                amountStock = new AmountStock();
            }
            amountStock.setCode(code);
            amountStock.setDate(date);
            amountStock.setClose(close);
            amountStock.setChg(chg);
            amountStock.setMainNetInflow(mainNetInflow);
            amountStock.setMainNetInflowRate(mainNetInflowRate);
            amountStock.setLargeNetInflow(largeNetInflow);
            amountStock.setLargeNetInflowRate(largeNetInflowRate);
            amountStock.setBigNetInflow(bigNetInflow);
            amountStock.setBigNetInflowRate(bigNetInflowRate);
            amountStock.setMidNetInflow(midNetInflow);
            amountStock.setMidNetInflowRate(midNetInflowRate);
            amountStock.setSmallNetInflow(smallNetInflow);
            amountStock.setSmallNetInflowRate(smallNetInflowRate);

            amountStockDao.save(amountStock);


        }
    }
}

